/*
 * Copyright (C) 2013 The i18nCTS Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.i18nCTS.util;

import android.content.Context;
import android.text.format.DateUtils;

import com.googlecode.i18nCTS.provider.i18nCTSProvider;

public class i18nCTSUtils {
	Context mContext = null;
	static int flags = 0;

	public i18nCTSUtils(Context context) {
		mContext = context;
	}

	@SuppressWarnings("deprecation")
	public
	String getParam(int flags) {
		String param = "";

		if ((DateUtils.FORMAT_SHOW_DATE & flags) == DateUtils.FORMAT_SHOW_DATE) param += "FORMAT_SHOW_DATE ";
		if ((DateUtils.FORMAT_SHOW_WEEKDAY & flags) == DateUtils.FORMAT_SHOW_WEEKDAY) param += "FORMAT_SHOW_WEEKDAY ";
		if ((DateUtils.FORMAT_SHOW_TIME & flags) == DateUtils.FORMAT_SHOW_TIME) param += "FORMAT_SHOW_TIME ";
		if ((DateUtils.FORMAT_NUMERIC_DATE & flags) == DateUtils.FORMAT_NUMERIC_DATE) param += "FORMAT_NUMERIC_DATE ";
		if ((DateUtils.FORMAT_SHOW_YEAR & flags) == DateUtils.FORMAT_SHOW_YEAR) param += "FORMAT_SHOW_YEAR ";
		if ((DateUtils.FORMAT_NO_YEAR & flags) == DateUtils.FORMAT_NO_YEAR) param += "FORMAT_NO_YEAR ";
		if ((DateUtils.FORMAT_NO_NOON_MIDNIGHT & flags) == DateUtils.FORMAT_NO_NOON_MIDNIGHT) 
			param += "FORMAT_NO_NOON_MIDNIGHT ";
		else {
			if ((DateUtils.FORMAT_NO_NOON & flags) == DateUtils.FORMAT_NO_NOON) param += "FORMAT_NO_NOON ";
			if ((DateUtils.FORMAT_NO_MIDNIGHT & flags) == DateUtils.FORMAT_NO_MIDNIGHT) param += "FORMAT_NO_MIDNIGHT ";
		}
		if ((DateUtils.FORMAT_NO_MONTH_DAY & flags) == DateUtils.FORMAT_NO_MONTH_DAY) param += "FORMAT_NO_MONTH_DAY ";
		if ((DateUtils.FORMAT_CAP_NOON_MIDNIGHT & flags) == DateUtils.FORMAT_CAP_NOON_MIDNIGHT) param += "FORMAT_CAP_NOON_MIDNIGHT ";
		if ((DateUtils.FORMAT_CAP_NOON & flags) == DateUtils.FORMAT_CAP_NOON) param += "FORMAT_CAP_NOON ";
		if ((DateUtils.FORMAT_CAP_MIDNIGHT & flags) == DateUtils.FORMAT_CAP_MIDNIGHT) param += "FORMAT_CAP_MIDNIGHT ";
		if ((DateUtils.FORMAT_CAP_AMPM & flags) == DateUtils.FORMAT_CAP_AMPM) param += "FORMAT_CAP_AMPM ";
		if ((DateUtils.FORMAT_ABBREV_WEEKDAY & flags) == DateUtils.FORMAT_ABBREV_WEEKDAY) param += "FORMAT_ABBREV_WEEKDAY ";
		if ((DateUtils.FORMAT_ABBREV_TIME & flags) == DateUtils.FORMAT_ABBREV_TIME) param += "FORMAT_ABBREV_TIME ";
		if ((DateUtils.FORMAT_ABBREV_RELATIVE & flags) == DateUtils.FORMAT_ABBREV_RELATIVE) param += "FORMAT_ABBREV_RELATIVE ";
		if ((DateUtils.FORMAT_ABBREV_MONTH & flags) == DateUtils.FORMAT_ABBREV_MONTH) param += "FORMAT_ABBREV_MONTH ";
		if ((DateUtils.FORMAT_ABBREV_ALL & flags) == DateUtils.FORMAT_ABBREV_ALL) param += "FORMAT_ABBREV_ALL ";
		if ((DateUtils.FORMAT_24HOUR & flags) == DateUtils.FORMAT_24HOUR) param += "FORMAT_24HOUR ";
		if ((DateUtils.FORMAT_12HOUR & flags) == DateUtils.FORMAT_12HOUR) param += "FORMAT_12HOUR ";

		return param;
	}
	
	void addDate(i18nCTSProvider provider, long time1, long time2, int flag) {
		int flags = DateUtils.FORMAT_SHOW_DATE;		
		if (flag != 0) flags |= flag;

		if (time1 == time2) 
			provider.addData(DateUtils.formatDateTime(mContext, time1, flags), getParam(flags));
		else
			provider.addData(DateUtils.formatDateRange(mContext, time1, time2, flags), getParam(flags));
	}	
	
	void addWeekday(i18nCTSProvider provider, long time1, long time2, int flag) {
		int flags = DateUtils.FORMAT_SHOW_WEEKDAY;
		if (flag != 0) flags |= flag;
		
		if (time1 == time2) 
			provider.addData(DateUtils.formatDateTime(mContext, time1, flags), getParam(flags));
		else
			provider.addData(DateUtils.formatDateRange(mContext, time1, time2, flags), getParam(flags));
	}	
	
	void addTime(i18nCTSProvider provider, long time1, long time2, int flag) {
		int flags = DateUtils.FORMAT_SHOW_TIME;
		if (flag != 0) flags |= flag;

		if (time1 == time2) 
			provider.addData(DateUtils.formatDateTime(mContext, time1, flags), getParam(flags));
		else
			provider.addData(DateUtils.formatDateRange(mContext, time1, time2, flags), getParam(flags));
	}	
	
	void addDateTime(i18nCTSProvider provider, long time1, long time2, int flag) {
		int flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE;
		if (flag != 0) flags |= flag;

		if (time1 == time2) 
			provider.addData(DateUtils.formatDateTime(mContext, time1, flags), getParam(flags));
		else
			provider.addData(DateUtils.formatDateRange(mContext, time1, time2, flags), getParam(flags));
	}	
	
	void addDateTime2(i18nCTSProvider provider, long time1, long time2, int flag) {
		flags = DateUtils.FORMAT_ABBREV_ALL;
		addDateTime(provider, time1, time2, (flag != 0) ? flags | flag : flags);
		
		flags = DateUtils.FORMAT_ABBREV_MONTH;
		addDateTime(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_TIME;
		addDateTime(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_ABBREV_MONTH;
		addDateTime(provider, time1, time2, (flag != 0) ? flags | flag : flags);
	}
	
	void addDateTimeWeekday(i18nCTSProvider provider, long time1, long time2, int flag) {
		int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY;
		if (flag != 0) flags |= flag;
		
		if (time1 == time2)
			provider.addData(DateUtils.formatDateTime(mContext, time1, flags), getParam(flags));
		else 
			provider.addData(DateUtils.formatDateRange(mContext, time1, time2, flags), getParam(flags));
	}
			
	void addDateTimeWeekday2(i18nCTSProvider provider, long time1, long time2, int flag) {
		flags = DateUtils.FORMAT_ABBREV_ALL;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);
		
		flags = DateUtils.FORMAT_ABBREV_MONTH;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_TIME;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_ABBREV_MONTH;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_WEEKDAY;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_ABBREV_WEEKDAY;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_WEEKDAY;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);

		flags = DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_ABBREV_WEEKDAY;
		addDateTimeWeekday(provider, time1, time2, (flag != 0) ? flags | flag : flags);
	}

    @SuppressWarnings("deprecation")
	public
	void buildDateTime(i18nCTSProvider provider, long time1, long time2) {
		addDate(provider, time1, time2, 0);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_MONTH);
		addDateTime(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR);
		addDateTime(provider, time1, time2, DateUtils.FORMAT_NO_YEAR);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_YEAR);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_SHOW_YEAR);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_NO_YEAR);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_NO_YEAR);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_NUMERIC_DATE);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTime(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTime(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
//		addDateTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
		addWeekday(provider, time1, time2, 0);
		addWeekday(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL);
		addWeekday(provider, time1, time2, DateUtils.FORMAT_ABBREV_WEEKDAY);
	
		addDateTime(provider, time1, time2, 0);
		addDateTime2(provider, time1, time2, 0);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_NUMERIC_DATE);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_12HOUR);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_12HOUR);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_24HOUR);
		addDateTime2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_24HOUR);
	
		addDateTimeWeekday(provider, time1, time2, 0);
		addDateTimeWeekday2(provider, time1, time2, 0);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_12HOUR);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_12HOUR);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_24HOUR);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_24HOUR);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_12HOUR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_12HOUR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_NUMERIC_DATE);
		addDateTimeWeekday2(provider, time1, time2, DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_NUMERIC_DATE);
		
		addTime(provider, time1, time2, 0);
		addTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_TIME);
		addTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL);
		addTime(provider, time1, time2, DateUtils.FORMAT_CAP_AMPM);
		addTime(provider, time1, time2, DateUtils.FORMAT_12HOUR);
		addTime(provider, time1, time2, DateUtils.FORMAT_24HOUR);
		addTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_TIME |DateUtils.FORMAT_12HOUR);
		addTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_12HOUR);
		addTime(provider, time1, time2, DateUtils.FORMAT_CAP_AMPM | DateUtils.FORMAT_12HOUR);
		addTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_TIME |DateUtils.FORMAT_24HOUR);
		addTime(provider, time1, time2, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_24HOUR);
		addTime(provider, time1, time2, DateUtils.FORMAT_CAP_AMPM | DateUtils.FORMAT_24HOUR);
	}

}
